-
Notifications
You must be signed in to change notification settings - Fork 449
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor ipsec function #4334
refactor ipsec function #4334
Conversation
9b0984d
to
094e677
Compare
Could you please add an e2e test case for this feature? I'm working on security: run as non-root user, which may break this function. |
e2e case will be added |
8834515
to
bd106fe
Compare
Signed-off-by: clyi <[email protected]>
Signed-off-by: clyi <[email protected]>
Signed-off-by: clyi <[email protected]>
Signed-off-by: clyi <[email protected]>
Signed-off-by: clyi <[email protected]>
Signed-off-by: clyi <[email protected]>
Signed-off-by: clyi <[email protected]>
Signed-off-by: clyi <[email protected]>
Signed-off-by: clyi <[email protected]>
Signed-off-by: clyi <[email protected]>
Signed-off-by: clyi <[email protected]>
f854bcd
to
9ee1ee5
Compare
Signed-off-by: clyi <[email protected]>
Signed-off-by: clyi <[email protected]>
Signed-off-by: clyi <[email protected]>
Signed-off-by: clyi <[email protected]>
Signed-off-by: clyi <[email protected]>
updateBanpQueue workqueue.RateLimitingInterface | ||
deleteBanpQueue workqueue.RateLimitingInterface | ||
banpKeyMutex keymutex.KeyMutex | ||
csrLister certListerv1.CertificateSigningRequestLister |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
need a empty line to separate.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
1
Signed-off-by: clyi <[email protected]>
refactor ovn ipsec function Signed-off-by: clyi <[email protected]>
// From this, point we are dealing with an approved CSR | ||
// Get CA in from ovn-ipsec-ca | ||
caSecret, err := c.config.KubeClient.CoreV1().Secrets("kube-system").Get(context.TODO(), util.DefaultOVNIPSecCA, metav1.GetOptions{}) | ||
if err != nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
need log err
|
||
// Decode the certificate request from PEM format. | ||
certReq, err := decodeCertificateRequest(csr.Spec.Request) | ||
if err != nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
need log err
|
||
// Decode the CA certificate from PEM format. | ||
caCert, err := decodeCertificate(caSecret.Data["cacert"]) | ||
if err != nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
need log err
} | ||
|
||
caKey, err := decodePrivateKey(caSecret.Data["cakey"]) | ||
if err != nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
need log err
// Create a new certificate using the certificate template and certificate. | ||
// We can then sign this using the CA. | ||
signedCert, err := signCSR(newCertificateTemplate(certReq), certReq.PublicKey, caCert, caKey) | ||
if err != nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
need log err
|
||
// Encode the certificate into PEM format and add to the status of the CSR | ||
csr.Status.Certificate, err = encodeCertificates(signedCert) | ||
if err != nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
need log err
return nil | ||
} | ||
|
||
if err := c.updateCsrStatus(csr); err != nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
need log err
Message: message, | ||
}) | ||
|
||
if err := c.updateCsrStatus(csr); err != nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
need log err
|
||
func newCertificateTemplate(certReq *x509.CertificateRequest) *x509.Certificate { | ||
serialNumber, err := rand.Int(rand.Reader, big.NewInt(1<<62)) | ||
if err != nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
need log err
|
||
func signCSR(template *x509.Certificate, requestKey c.PublicKey, issuer *x509.Certificate, issuerKey c.PrivateKey) (*x509.Certificate, error) { | ||
derBytes, err := x509.CreateCertificate(rand.Reader, template, issuer, requestKey, issuerKey) | ||
if err != nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
need log err
return nil, err | ||
} | ||
certs, err := x509.ParseCertificates(derBytes) | ||
if err != nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
need log err
|
||
func decodeCertificateRequest(pemBytes []byte) (*x509.CertificateRequest, error) { | ||
block, _ := pem.Decode(pemBytes) | ||
if block == nil || block.Type != "CERTIFICATE REQUEST" { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
need log err
func decodeCertificate(pemBytes []byte) (*x509.Certificate, error) { | ||
block, _ := pem.Decode(pemBytes) | ||
if block == nil || block.Type != "CERTIFICATE" { | ||
err := errors.New("PEM block type must be CERTIFICATE") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
need log err
func decodePrivateKey(pemBytes []byte) (*rsa.PrivateKey, error) { | ||
block, _ := pem.Decode(pemBytes) | ||
if block == nil || block.Type != "PRIVATE KEY" { | ||
fmt.Println(block.Type) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
need log err
} | ||
|
||
key, err := x509.ParsePKCS8PrivateKey(block.Bytes) | ||
if err != nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
need log err
func encodeCertificates(certs ...*x509.Certificate) ([]byte, error) { | ||
b := bytes.Buffer{} | ||
for _, cert := range certs { | ||
if err := pem.Encode(&b, &pem.Block{Type: "CERTIFICATE", Bytes: cert.Raw}); err != nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
need log err
func getOVSSystemID() (string, error) { | ||
cmd := exec.Command("ovs-vsctl", "--retry", "-t", "60", "get", "Open_vSwitch", ".", "external-ids:system-id") | ||
output, err := cmd.Output() | ||
if err != nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
need log err
|
||
func checkCertExpired() (bool, error) { | ||
certBytes, err := os.ReadFile(ipsecCertPath) | ||
if err != nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
need log err
} | ||
|
||
cert, err := x509.ParseCertificate(block.Bytes) | ||
if err != nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
need log err
|
||
func generateCSRCode() ([]byte, error) { | ||
cn, err := getOVSSystemID() | ||
if err != nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
need log err
klog.Infof("ovs system id: %s", cn) | ||
cmd := exec.Command("openssl", "genrsa", "-out", ipsecPrivKeyPath, "2048") | ||
err = cmd.Run() | ||
if err != nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
need log err
|
||
func linkCACertToIPSecDir() error { | ||
cmd := exec.Command("ln", "-s", ipsecCACertPath, "/etc/ipsec.d/cacerts/") | ||
if err := cmd.Run(); err != nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
need log err
func clearCACertToIPSecDir() error { | ||
// clear /etc/openvswitch/keys/ipsec-cacert.pem | ||
cmd := exec.Command("rm", "-f", "/etc/openvswitch/keys/ipsec-cacert.pem") | ||
if err := cmd.Run(); err != nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
need log err
} | ||
|
||
func initIPSecKeysDir() error { | ||
if err := os.MkdirAll(ipsecKeyDir, 0o755); err != nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
need log err
} | ||
|
||
func clearIPSecKeysDir() error { | ||
if err := os.Remove(ipsecPrivKeyPath); err != nil && !os.IsNotExist(err) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
need log err
} | ||
} else { | ||
checkCertExpired, err := checkCertExpired() | ||
if err != nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
need log err
@changluyi 大佬,我感觉有些err log 还是就近打印更好些,后面有空麻烦补充下 |
好的 我有空加下 |
Pull Request
What type of this PR
Examples of user facing changes:
Features
Bug fixes
Docs
Tests
Which issue(s) this PR fixes
Fixes #(issue-number)